package com.example.tianqi;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class weatherAdapter extends RecyclerView.Adapter<weatherAdapter.ViewHolder> {

    private final ArrayList<JSONObject> arrayList;

    public weatherAdapter(ArrayList<JSONObject> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //在此处 写 每一个子项需要执行的操作
        JSONObject jsonObject = arrayList.get(position);
        try {
            holder.win2TextView.append(jsonObject.getString("win"));
            holder.winspeed2TextView.append(jsonObject.getString("win_speed"));
            holder.shidu2TextView.append(jsonObject.getString("humidity"));
            holder.air2TextView.append(jsonObject.getString("air"));
            holder.air2TextView.append(" " + jsonObject.getString("air_level"));
            holder.pressure2ViewTextView.append(jsonObject.getString("pressure")+"hPa");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_weather, group, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final AppCompatTextView win2TextView;
        private final AppCompatTextView winspeed2TextView;
        private final AppCompatTextView shidu2TextView;
        private final AppCompatTextView air2TextView;
        private final AppCompatTextView pressure2ViewTextView;

        private ViewHolder(View view) {
            super(view);
            win2TextView = view.findViewById(R.id.win2TextView);
            winspeed2TextView = view.findViewById(R.id.winspeed2TextView);
            shidu2TextView = view.findViewById(R.id.shidu2TextView);
            air2TextView = view.findViewById(R.id.air2TextView);
            pressure2ViewTextView = view.findViewById(R.id.pressure2ViewTextView);
        }
    }
}
